/**
 * @fileOverview
 * @exports Model as org.puremvc.js.core.Model
 */

/**
 * @constructor
 */
function Model ()
{
	if (null != Model.instance)
	{
		throw new Error (Model.SINGLETON_MSG);
	}
	
	Model.instance= this;
	this.proxyMap= {};
	this.initializeModel();
};

/**
 * @protected
 * @return {void}
 */
Model.prototype.initializeModel= function () {};

/**
 * @static
 * @return {Model}
 * @throws {Error}
 */
Model.getInstance= function ()
{
	if (null == Model.instance)
	{
		// N.B. The original AS3 implementation
		// is followed exactly here- note the
		// double assignment of the Model instance
		// here AND in the constructor.
		Model.instance= new Model();
	}
	
	return Model.instance;
};

/**
 * 
 * @param {Proxy} proxy
 * @return {void}
 */
Model.prototype.registerProxy= function (proxy)
{
	// TODO confirm with Cliff why Proxys can potentially register multiple times, but mediators cannot
	this.proxyMap[proxy.getProxyName()]= proxy;
	proxy.onRegister();
};

/**
 * @param {string} proxyName
 * @return {?Proxy}
 */
Model.prototype.retrieveProxy= function (proxyName)
{
	return this.proxyMap[proxyName];
};

/**
 * @param {string} proxyName
 * @return {boolean}
 */
Model.prototype.hasProxy= function (proxyName)
{
	return this.proxyMap[proxyName] != null;
};

/**
 * @todo Discuss with Cliff why the proxy is not deleted from proxyMap, as opposed to being assigned null
 * @param {string} proxyName
 * @return {?Proxy}
 */
Model.prototype.removeProxy= function (proxyName)
{
	var proxy= this.proxyMap[proxyName];
	if (proxy)
	{
		this.proxyMap[proxyName]= null;
		proxy.onRemove();
	}
	
	return proxy;
};

/**
 * @protected
 * @static
 * @type {Object}
 */
Model.prototype.proxyMap= null;

/**
 * @protected
 * @static
 * @type {Model}
 */
Model.instance= null;

/**
 * @protected
 * @static
 * @const
 * @type {string}
 */
Model.SINGLETON_MSG= "Model singleton already constructed";

